export function StringBuffer() {
    this.__strings__ = new Array();
}


/**
 * 字符串格式化工具
 * @param args  {0} {1}被替换的参数
 * @returns {String}
 */
String.prototype.format = function (args) {
    let result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (let key in args) {
                if (args[key] != undefined) {
                    let reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        } else {
            for (let i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    let reg = new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
};

StringBuffer.prototype.append = function (str) {
    this.__strings__.push(str);
    return this;    //方便链式操作
};
StringBuffer.prototype.toString = function () {
    return this.__strings__.join("");
};

export default class StringUtils {

    static setAreaId(id) {
        let buffer = new StringBuffer();
        buffer.append('area');
        buffer.append(' ');
        buffer.append(id);
        return buffer.toString();
    }

    static setDayId(id) {
        let buffer = new StringBuffer();
        buffer.append('day');
        buffer.append(' ');
        buffer.append(id);
        return buffer.toString();
    }

    static getDayId(stringDay) {
        return stringDay.split(' ')[1];
    }

    static getAreaId(stringArea) {
        return stringArea;
    }

    static setLogicId(id) {
        let buffer = new StringBuffer();
        buffer.append('logic');
        buffer.append(' ');
        buffer.append(id);
        return buffer.toString();
    }

    static getLogicId(stringArea) {
        return stringArea.split(' ')[1];
    }

    static setMemberId(id) {
        let buffer = new StringBuffer();
        buffer.append('member');
        buffer.append(' ');
        buffer.append(id);
        return buffer.toString();
    }

    static getMemberId(string) {
        return string.split(' ')[1];
    }

    static setGatewayId(id) {
        let buffer = new StringBuffer();
        buffer.append('gateway');
        buffer.append(' ');
        buffer.append(id);
        return buffer.toString();
    }

    static getGatewayId(string) {
        return string.split(' ')[1];
    }

    /**
     * 自动生成WSMessage中的id
     * '0:0:0:name+randomNum'
     */
    static generateWsId() {
        let buffer = new StringBuffer();
        buffer.append(0).append(':').append(0).append(':').append(0).append(':').append('web-').append('00001111');
        return buffer.toString();
    }


    /**
     * 自动生成WSMessage中的id
     * '0:0:0:0
     * DevId : SlaveId : GWId : EdgeId
     * @param id
     * @returns {*}
     */
    static generateWsTid = (...id) => {
        if (id.length > 4) {
            throw Error("The length of id to generate tid > 4")
        }

        let buffer = new StringBuffer();

        if (id.length < 4) {
            for (let i = 0; i < 4 - id.length; i++) {
                if (i === 3) {
                    buffer.append(0)
                } else {
                    buffer.append(0).append(':');
                }
            }
        }
        id.map((value, index) => {
            if (index !== id.length - 1) {
                buffer.append(value).append(':')
            } else {
                buffer.append(value);
            }
        });
        return buffer.toString();
    };

    /**
     * 从tid中获取指定信息
     * @param tid
     * @param num
     * @returns {*}
     */
    static getDevIdFromTid(tid, num) {
        if (num > 3 || !tid) {
            return -1;
        }
        let temp = tid.split(':');
        if (temp.length < num) {
            return -1
        }
        return temp[num]
    }

    /**
     * 数字转星期
     * @param num
     * @returns {string}
     */
    static numToDay(num) {
        let day;
        switch (num) {
            case '1':
                day = '周一';
                break;
            case '2':
                day = '周二';
                break;
            case '3':
                day = '周三';
                break;
            case '4':
                day = '周四';
                break;
            case '5':
                day = '周五';
                break;
            case '6':
                day = '周六';
                break;
            case '0':
                day = '周日';
                break;
            default:
                day = '周一';
                break;
        }

        return day;
    }
}
